package com.example.administrator.recyclerviewtest_422zwj.model;


import com.example.administrator.recyclerviewtest_422zwj.bean.TcaseBean;
import com.example.administrator.recyclerviewtest_422zwj.iface.ArticleListener;
import com.example.administrator.recyclerviewtest_422zwj.iface.Articleiface;
import com.example.administrator.recyclerviewtest_422zwj.iface.TcaseListener;
import com.example.administrator.recyclerviewtest_422zwj.iface.Tcaseiface;
import com.example.administrator.recyclerviewtest_422zwj.service.ArticleService;
import com.example.administrator.recyclerviewtest_422zwj.service.TcaseService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/4/20 0020.
 */
//model  获取文章数据
public class TcaseModel implements Tcaseiface{
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public TcaseModel()
    {
        //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResultList(String tcase, int page, String sessionID, final TcaseListener tcaseListener) {
        //使用Retrofit----2
        TcaseService tcaseService
                =retrofit.create(TcaseService.class);
        /*Call<List<TcaseBean>> call
                =tcaseService.getResult(mod,page,sessionID);*/
        Call<List<TcaseBean>> call
                =tcaseService.getResult(tcase,page,sessionID);
        //使用Retrofit----3
        call.enqueue(new Callback<List<TcaseBean>>() {
            @Override
            public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                if (response.isSuccessful()&&response!=null)
                {
                    tcaseListener.onResponse(response.body());
                }
                else tcaseListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                tcaseListener.onFail(t.toString());
            }
        });

    }
}
